package mm.apk.libmm.exts

import android.app.Activity
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import mm.apk.libmm.constant.LibConfig.BtnClickEventTimeTagKey

/**
 * Descriptions：
 * StartVersion：
 * <p>
 * Author：ChenME
 * Date：2019-11-15
 * Email：ibelieve1210@163.com
 */

/**
 * 扩展 View 的点击事件
 */
fun View.onClick(isUseDelay: Boolean = true, method: () -> Unit) {
    this.setOnClickListener {
        if (isUseDelay) {
            val vTag = this.getTag(BtnClickEventTimeTagKey)

            val currentClick = System.currentTimeMillis()
            if (vTag is Long) {
                if (currentClick - vTag > 700) {
                    this.setTag(BtnClickEventTimeTagKey, currentClick)
                    method()
                }
            } else {
                this.setTag(BtnClickEventTimeTagKey, currentClick)
                method()
            }
        } else {
            method()
        }
    }
}

/**
 * 扩展 View 的可见性
 */
fun View.setVisible(isShow: Boolean) {
    this.visibility = if (isShow) View.VISIBLE else View.GONE
}

/**
 * 扩展 View 的可见性
 */
fun View.setInVisible(isShow: Boolean) {
    this.visibility = if (isShow) View.VISIBLE else View.INVISIBLE
}

/**
 * 判断 View 是否可见
 */
fun View.isVisible(): Boolean {
    return this.visibility == View.VISIBLE
}

/**
 * 设置 View 的 Margin
 */
fun View.setMargin(left: Int, top: Int, right: Int, bottom: Int) {
    val lp = layoutParams as ViewGroup.MarginLayoutParams
    lp.setMargins(left, top, right, bottom)
    requestLayout()
}

/**
 * 通过 start、end、top、bottom 来设置 view 的 Margin
 */
fun View.setMarginByStart(start: Int? = null, end: Int? = null, top: Int? = null, bottom: Int? = null) {
    val lp = layoutParams as ViewGroup.MarginLayoutParams
    start?.apply { lp.marginStart = this }
    end?.apply { lp.marginEnd = this }
    top?.apply { lp.topMargin = this }
    bottom?.apply { lp.bottomMargin = this }
}

/**
 * 获取 EditText 的文字
 */
fun EditText.fetchText(): String {
    return this.text.toString()
}

/**
 * 设置 EditText 输入类型为密码
 */
fun EditText.inputPassword() {
    this.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
}

/**
 * EditText 获取焦点并弹出键盘
 */
fun EditText.focus(activity: Activity, delayTimeMillis: Long = 700) {
    this.isFocusable = true
    this.isFocusableInTouchMode = true
    this.requestFocus()
    GlobalScope.launch(Dispatchers.IO) {
        delay(delayTimeMillis)
        launch(Dispatchers.Main) {
            activity.inputMethodManager?.showSoftInput(this@focus, 0)
        }
    }
}

/**
 * EditText 失去焦点
 */
fun EditText.unFocus(activity: Activity) {
    val father = this.parent
    if (father is View) {
        father.isFocusable = true
        father.isFocusableInTouchMode = true
        activity.hideKeyboard(this)
    }
}

/**
 * 设置 EditText 的文字更改的监听
 */
fun EditText.lisenTextChange(watcher: (s: CharSequence?, start: Int, before: Int, count: Int) -> Unit) {
    this.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {}

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            watcher(s, start, before, count)
        }
    })
}
